﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DBAccessLib;
using System.IO.Ports;

namespace LineWatchSimple.WinForm
{
    public partial class ProductManagementView : Form
    {
        ScreenSaver screenSaver = null;
        LineInfoSetView lineInfoSetView = null;
        ProductNumSetView productNumSetView = null;
        SerialPort port = null;
        bool isOn = false;

        /// <summary>
        /// Constructor
        /// </summary>
        public ProductManagementView()
        {
            InitializeComponent();

            this.timerReadLineInfo.Enabled = false;
            this.timerReadProductInfo.Enabled = false;
            this.timerUpdateProductInfo.Enabled = false;

            string defaultValue = "0";

            this.lblDayPlanLine1.Text = defaultValue;
            this.lblCurrentPlanLine1.Text = defaultValue;
            this.lblActualCompletedLine1.Text = defaultValue;
            this.lblDifferenceLine1.Text = defaultValue;

            this.lblDayPlanLine2.Text = defaultValue;
            this.lblCurrentPlanLine2.Text = defaultValue;
            this.lblActualCompletedLine2.Text = defaultValue;
            this.lblDifferenceLine2.Text = defaultValue;

            this.lblDayPlanLine3.Text = defaultValue;
            this.lblCurrentPlanLine3.Text = defaultValue;
            this.lblActualCompletedLine3.Text = defaultValue;
            this.lblDifferenceLine3.Text = defaultValue;

            if (ConfigurationManager.AppSettings["IsProductPC"].ToString()!="F")
            {
                port = new SerialPort("COM1", 9600, Parity.None);
                port.Open();
                port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            }
            this.FormClosing += new FormClosingEventHandler(ProductManagementView_FormClosing);
        }

        void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                string t = string.Empty;
                int outT;
                if (!int.TryParse(t, out outT)) outT = 200;
                Thread.Sleep(outT);
                byte[] readBuffer = new byte[6];
                port.Read(readBuffer, 0, readBuffer.Length);
                byte check1 = readBuffer[0], check2 = readBuffer[1], check3 = readBuffer[2];
                byte num1 = readBuffer[3];
                if (check1 == 1 && check2 == 2 && check3 == 1)
                {
                    if (num1 == 14) isOn = true;
                    else if (num1 == 15 && isOn)
                    {
                        isOn = false;
                        this.Invoke(new Action(() => RefreshProductInfoAndUpdateUI()));
                    }
                }
            }
            catch (Exception)
            {
                
            }
            
        }

        /// <summary>
        /// Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProductManagementView_Load(object sender, EventArgs e)
        {
            ConfigInfo.InitConfigInfo();
            this.InitOperationArea();
            this.GetLineInfoAndUpdateUI();
            this.GetProductInfoAndUpdateUI();
            this.InitLineTitle();

            this.InitFont();
        }

        /// <summary>
        /// Shown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProductManagementView_Shown(object sender, EventArgs e)
        {
            if (ConfigInfo.IsProductPC)
            {
                this.timerReadLineInfo.Enabled = true;
                this.timerReadProductInfo.Enabled = true;
                this.timerUpdateProductInfo.Enabled = ConfigInfo.IsDemoTimerEnable;
                this.btnLineSetting.Visible = false;
                this.btnProductSetting.Visible = false;
            }
            else
            {
                this.timerReadLineInfo.Enabled = true;
                this.timerReadProductInfo.Enabled = true;
                this.timerUpdateProductInfo.Enabled = false;
                this.btnLineSetting.Visible = true;
                this.btnProductSetting.Visible = true;
            }

            this.timerReadLineInfo.Interval = ConfigInfo.RefreshLineInfoSpan;
            this.timerReadProductInfo.Interval = ConfigInfo.RefreshProductInfoSpan;
            this.timerUpdateProductInfo.Interval = ConfigInfo.UpdateDemoProductInfoSpan;

            this.timerScreenSaver.Enabled = ConfigInfo.IsUseScreenSaver;
            this.timerScreenSaver.Interval = ConfigInfo.ShowScreenSaverSpan;
        }

        /// <summary>
        /// Init Operation Area
        /// </summary>
        private void InitOperationArea()
        {
            if (ConfigInfo.WatchLines.Contains(ConfigInfo.LineCode1)
                && ConfigInfo.WatchLines.Contains(ConfigInfo.LineCode2)
                && ConfigInfo.WatchLines.Contains(ConfigInfo.LineCode3))
            {
                this.tlpOperationArea1.Visible = true;
                this.tlpOperationArea2.Visible = true;
                this.tlpOperationArea3.Visible = true;

                this.tlpMain.ColumnStyles.Clear();
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
            }
            else if (ConfigInfo.WatchLines.Contains(ConfigInfo.LineCode1)
                     && ConfigInfo.WatchLines.Contains(ConfigInfo.LineCode2))
            {
                this.tlpOperationArea1.Visible = true;
                this.tlpOperationArea2.Visible = true;
                this.tlpOperationArea3.Visible = false;

                this.tlpMain.ColumnStyles.Clear();
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 69F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 31F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
            }
            else if (ConfigInfo.WatchLines.Contains(ConfigInfo.LineCode1)
                     && ConfigInfo.WatchLines.Contains(ConfigInfo.LineCode3))
            {
                this.tlpOperationArea1.Visible = true;
                this.tlpOperationArea2.Visible = false;
                this.tlpOperationArea3.Visible = true;

                this.tlpMain.ColumnStyles.Clear();
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
            }
            else if (ConfigInfo.WatchLines.Contains(ConfigInfo.LineCode2)
                     && ConfigInfo.WatchLines.Contains(ConfigInfo.LineCode3))
            {
                this.tlpOperationArea1.Visible = false;
                this.tlpOperationArea2.Visible = true;
                this.tlpOperationArea3.Visible = true;

                this.tlpMain.ColumnStyles.Clear();
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
            }
            else if (ConfigInfo.WatchLines.Contains(ConfigInfo.LineCode1))
            {
                this.tlpOperationArea1.Visible = true;
                this.tlpOperationArea2.Visible = false;
                this.tlpOperationArea3.Visible = false;

                this.tlpMain.ColumnStyles.Clear();
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 30F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 30F));
            }
            else if (ConfigInfo.WatchLines.Contains(ConfigInfo.LineCode2))
            {
                this.tlpOperationArea1.Visible = true;
                this.tlpOperationArea2.Visible = true;
                this.tlpOperationArea3.Visible = false;

                this.tlpMain.ColumnStyles.Clear();
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 40F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 60F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 30F));
                lblProductManagement1.Visible = false;
            }
            else if (ConfigInfo.WatchLines.Contains(ConfigInfo.LineCode3))
            {
                this.tlpOperationArea1.Visible = false;
                this.tlpOperationArea2.Visible = false;
                this.tlpOperationArea3.Visible = true;

                this.tlpMain.ColumnStyles.Clear();
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 30F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 30F));
            }
        }

        /// <summary>
        /// Init Font
        /// </summary>
        private void InitFont()
        {
            if (ConfigInfo.IsUseCustomizedFont)
            {
                foreach (Control control in this.Controls)
                {
                    if (control.GetType() == typeof(Label))
                    {
                        Label lbl = (Label)control;
                        if (lbl.Tag != null)
                        {
                            this.UpdateFont(lbl);
                        }
                    }

                    InitFont(control);
                }
            }
        }

        /// <summary>
        /// Init Font
        /// </summary>
        /// <param name="controlParent"></param>
        private void InitFont(Control controlParent)
        {
            foreach (Control control in controlParent.Controls)
            {
                if (control.GetType() == typeof(Label))
                {
                    Label lbl = (Label)control;
                    if (lbl.Tag != null)
                    {
                        this.UpdateFont(lbl);
                    }
                }

                InitFont(control);
            }
        }

        /// <summary>
        /// Update Font
        /// </summary>
        /// <param name="control"></param>
        private void UpdateFont(Control control)
        {
            if (control.Tag.ToString().StartsWith("FontSizeTitle"))
            {
                this.SetFont(control, "Microsoft YaHei", ConfigInfo.FontSizeTitle, FontStyle.Bold);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeCaption"))
            {
                this.SetFont(control, "Microsoft YaHei", ConfigInfo.FontSizeCaption, FontStyle.Bold);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeValue"))
            {
                this.SetFont(control, "Impact", ConfigInfo.FontSizeValue, FontStyle.Regular);
            }
        }

        /// <summary>
        /// Set Font
        /// </summary>
        /// <param name="control"></param>
        /// <param name="familyName"></param>
        /// <param name="emSize"></param>
        /// <param name="style"></param>
        private void SetFont(Control control, string familyName, float emSize, FontStyle style)
        {
            control.Font = new System.Drawing.Font(familyName,
                                                   emSize,
                                                   style,
                                                   System.Drawing.GraphicsUnit.Point,
                                                   ((byte)(0))
                                                   );
        }

        /// <summary>
        /// Init Line Title
        /// </summary>
        private void InitLineTitle()
        {
            string productManagement = "线号：";

            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode1))
            {
                this.lblProductManagement1.Text = productManagement + ConfigInfo.LineName1;
            }

            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode2))
            {
                this.lblProductManagement2.Text = productManagement + ConfigInfo.LineName2;
            }

            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode3))
            {
                this.lblProductManagement3.Text = productManagement + ConfigInfo.LineName3;
            }
        }

        /// <summary>
        /// Update Line Info
        /// </summary>
        /// <param name="machineType"></param>
        /// <param name="planNum"></param>
        /// <param name="lineCode"></param>
        private void UpdateLineInfo(string machineType, long planNum, string lineCode)
        {
            long nowPlan = 0;

            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                this.lblMachineTypeLine1.Text = machineType;

                this.lblDayPlanLine1.Text = planNum.ToString();

                nowPlan = ConfigInfo.GetNowPlanInfo(ConfigInfo.DtLineShift1, planNum);
                this.lblCurrentPlanLine1.Text = nowPlan.ToString();

                long productNum = 0;
                long.TryParse(lblActualCompletedLine1.Text.Trim(), out productNum);
                this.lblDifferenceLine1.Text = (productNum - nowPlan).ToString();
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                this.lblMachineTypeLine2.Text = machineType;

                this.lblDayPlanLine2.Text = planNum.ToString();
                nowPlan = ConfigInfo.GetNowPlanInfo(ConfigInfo.DtLineShift2, planNum);
                this.lblCurrentPlanLine2.Text = nowPlan.ToString();

                long productNum = 0;
                long.TryParse(lblActualCompletedLine2.Text.Trim(), out productNum);
                this.lblDifferenceLine2.Text = (productNum - nowPlan).ToString();
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                this.lblMachineTypeLine3.Text = machineType;

                this.lblDayPlanLine3.Text = planNum.ToString();
                nowPlan = ConfigInfo.GetNowPlanInfo(ConfigInfo.DtLineShift3, planNum);
                this.lblCurrentPlanLine3.Text = nowPlan.ToString();

                long productNum = 0;
                long.TryParse(lblActualCompletedLine3.Text.Trim(), out productNum);
                this.lblDifferenceLine3.Text = (productNum - nowPlan).ToString();
            }

            this.Refresh();
            this.Update();
        }

        /// <summary>
        /// Update Product Info
        /// </summary>
        /// <param name="productNum"></param>
        /// <param name="lineCode"></param>
        private void UpdateProductInfo(long productNum, string lineCode)
        {
            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                if (productNum >= 0)
                {
                    this.lblActualCompletedLine1.Text = productNum.ToString();
                }

                long nowPlan = 0;
                long.TryParse(this.lblCurrentPlanLine1.Text.Trim(), out nowPlan);
                this.lblDifferenceLine1.Text = (productNum - nowPlan).ToString();
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                if (productNum >= 0)
                {
                    this.lblActualCompletedLine2.Text = productNum.ToString();
                }

                long nowPlan = 0;
                long.TryParse(this.lblCurrentPlanLine2.Text.Trim(), out nowPlan);
                this.lblDifferenceLine2.Text = (productNum - nowPlan).ToString();
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                if (productNum >= 0)
                {
                    this.lblActualCompletedLine3.Text = productNum.ToString();
                }

                long nowPlan = 0;
                long.TryParse(this.lblCurrentPlanLine3.Text.Trim(), out nowPlan);
                this.lblDifferenceLine3.Text = (productNum - nowPlan).ToString();
            }

            this.Refresh();
            this.Update();
        }

        /// <summary>
        /// Change Shift Name
        /// </summary>
        /// <returns></returns>
        private bool ChangeShiftName()
        {
            bool isShiftNameChanged = false;

            string shiftInfoLine1 = string.Empty;
            string shiftInfoLine2 = string.Empty;
            string shiftInfoLine3 = string.Empty;

            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode1))
            {
                shiftInfoLine1 = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineShift1);

                if (!string.Equals(this.lblShiftLine1.Text.Trim(), shiftInfoLine1.Trim()))
                {
                    isShiftNameChanged = true;
                }
            }

            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode2))
            {
                shiftInfoLine2 = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineShift2);

                if (!string.Equals(this.lblShiftLine2.Text.Trim(), shiftInfoLine2.Trim()))
                {
                    isShiftNameChanged = true;
                }
            }

            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode3))
            {
                shiftInfoLine3 = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineShift3);

                if (!string.Equals(this.lblShiftLine3.Text.Trim(), shiftInfoLine3.Trim()))
                {
                    isShiftNameChanged = true;
                }
            }

            if (isShiftNameChanged)
            {
                this.lblShiftLine1.Text = shiftInfoLine1.Trim();
                this.lblShiftLine2.Text = shiftInfoLine2.Trim();
                this.lblShiftLine3.Text = shiftInfoLine3.Trim();
            }

            return isShiftNameChanged;
        }

        /// <summary>
        /// Get Shift
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private string GetShift(string text)
        {
            if (text.Contains("1"))
            {
                return "1";
            }
            else if (text.Contains("2"))
            {
                return "2";
            }

            return "0";
        }

        /// <summary>
        /// Qiut
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Form Closing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProductManagementView_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(port!=null&&port.IsOpen)port.Close();
        }

        /// <summary>
        /// Get Line Info And Update UI
        /// </summary>
        public void GetLineInfoAndUpdateUI()
        { 
            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode1))
            {
                string NumTime1 = LineShift.GetLineNumTime(ConfigInfo.LineCode1);
                ConfigInfo.DtLineInfo1 = LineInfo.GetLineInfo_New(ConfigInfo.LineCode1, NumTime1);
                ConfigInfo.DtLineShift1 = LineShift.GetLineShift_New(ConfigInfo.LineCode1, NumTime1);
                long planNum = ConfigInfo.GetShiftPlanNum(ConfigInfo.DtLineShift1, NumTime1.Trim());

                string machineType = string.Empty;
                if (ConfigInfo.DtLineInfo1.Rows.Count >= 1)
                {
                    machineType = ConfigInfo.DtLineInfo1.Rows[0]["MachineType"].ToString().Trim();
                }

                this.UpdateLineInfo(machineType, planNum, ConfigInfo.LineCode1);
            }

            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode2))
            {
                string NumTime2 = LineShift.GetLineNumTime(ConfigInfo.LineCode2);
                ConfigInfo.DtLineInfo2 = LineInfo.GetLineInfo_New(ConfigInfo.LineCode2, NumTime2);
                ConfigInfo.DtLineShift2 = LineShift.GetLineShift_New(ConfigInfo.LineCode2, NumTime2);
                long planNum = ConfigInfo.GetShiftPlanNum(ConfigInfo.DtLineShift2, NumTime2.Trim());

                string machineType = string.Empty;
                if (ConfigInfo.DtLineInfo2.Rows.Count >= 1)
                {
                    machineType = ConfigInfo.DtLineInfo2.Rows[0]["MachineType"].ToString().Trim();
                }

                this.UpdateLineInfo(machineType, planNum, ConfigInfo.LineCode2);
            }

            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode3))
            {
                string NumTime3 = LineShift.GetLineNumTime(ConfigInfo.LineCode3);
                ConfigInfo.DtLineInfo3 = LineInfo.GetLineInfo_New(ConfigInfo.LineCode3, NumTime3);
                ConfigInfo.DtLineShift3 = LineShift.GetLineShift_New(ConfigInfo.LineCode3, NumTime3);
                long planNum = ConfigInfo.GetShiftPlanNum(ConfigInfo.DtLineShift3, NumTime3.Trim());

                string machineType = string.Empty;
                if (ConfigInfo.DtLineInfo3.Rows.Count >= 1)
                {
                    machineType = ConfigInfo.DtLineInfo3.Rows[0]["MachineType"].ToString().Trim();
                }

                this.UpdateLineInfo(machineType, planNum, ConfigInfo.LineCode3);
            }

            this.ChangeShiftName();
        }

        /// <summary>
        /// Get Product Info And Update UI
        /// </summary>
        public void GetProductInfoAndUpdateUI()
        {
            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode1))
            {
                string NumTime1 = LineShift.GetLineNumTime(ConfigInfo.LineCode1);
                ConfigInfo.DtLineShift1 = LineShift.GetLineShift_New(ConfigInfo.LineCode1, NumTime1);
                //DateTime currentWorkDate = ConfigInfo.GetCurrentWorkDate(ConfigInfo.DtLineShift1);
                string shift = ConfigInfo.GetShift(ConfigInfo.DtLineShift1);
                ConfigInfo.DtProductInfo1 = ProductInfo.GetProductInfo(ConfigInfo.LineCode1, shift, NumTime1);
                long productNum = ConfigInfo.GetShiftProductNum(shift, ConfigInfo.DtProductInfo1);

                this.UpdateProductInfo(productNum, ConfigInfo.LineCode1);
            }

            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode2))
            {
                string NumTime2 = LineShift.GetLineNumTime(ConfigInfo.LineCode2);
                ConfigInfo.DtLineShift2 = LineShift.GetLineShift_New(ConfigInfo.LineCode2, NumTime2);
                //DateTime currentWorkDate = ConfigInfo.GetCurrentWorkDate(ConfigInfo.DtLineShift2);
                string shift = ConfigInfo.GetShift(ConfigInfo.DtLineShift2);
                ConfigInfo.DtProductInfo2 = ProductInfo.GetProductInfo(ConfigInfo.LineCode2, shift, NumTime2);
                long productNum = ConfigInfo.GetShiftProductNum(shift, ConfigInfo.DtProductInfo2);

                this.UpdateProductInfo(productNum, ConfigInfo.LineCode2);
            }

            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode3))
            {
                string NumTime3 = LineShift.GetLineNumTime(ConfigInfo.LineCode3);
                ConfigInfo.DtLineShift3 = LineShift.GetLineShift_New(ConfigInfo.LineCode3, NumTime3);
                //DateTime currentWorkDate = ConfigInfo.GetCurrentWorkDate(ConfigInfo.DtLineShift3);
                string shift = ConfigInfo.GetShift(ConfigInfo.DtLineShift3);
                ConfigInfo.DtProductInfo3 = ProductInfo.GetProductInfo(ConfigInfo.LineCode3, shift, NumTime3);
                long productNum = ConfigInfo.GetShiftProductNum(shift, ConfigInfo.DtProductInfo3);

                this.UpdateProductInfo(productNum, ConfigInfo.LineCode3);
            }
        }

        /// <summary>
        /// Refresh Product Info And Save
        /// </summary>
        private void RefreshProductInfoAndSave()
        {
            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode1))
            {
                string NumTime1 = LineShift.GetLineNumTime(ConfigInfo.LineCode1);
                string productNumTemp = this.lblActualCompletedLine1.Text.Trim();
                long productNum = 0;

                long.TryParse(productNumTemp, out productNum);
                productNum++;

                this.UpdateProductInfo(productNum, ConfigInfo.LineCode1);

                if (ConfigInfo.DtProductInfo1.Rows.Count >= 1)
                {
                    foreach (DataRow row in ConfigInfo.DtProductInfo1.Rows)
                    {
                        row["LineCode"] = ConfigInfo.LineCode1;
                        row["Shift"] = ConfigInfo.GetShift(ConfigInfo.DtLineShift1);
                        row["ProductNum"] = productNum;
                        row["NumTime"] = NumTime1;
                    }
                }
                else
                {
                    DataRow row = ConfigInfo.DtProductInfo1.NewRow();
                    row["LineCode"] = ConfigInfo.LineCode1;
                    row["Shift"] = ConfigInfo.GetShift(ConfigInfo.DtLineShift1);
                    row["ProductNum"] = productNum;
                    row["NumTime"] = NumTime1;

                    ConfigInfo.DtProductInfo1.Rows.Add(row);
                }

                ProductInfo.UpdateProductInfo(ConfigInfo.DtProductInfo1);
            }

            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode2))
            {
                string NumTime2 = LineShift.GetLineNumTime(ConfigInfo.LineCode2);
                string productNumTemp = this.lblActualCompletedLine2.Text.Trim();
                long productNum = 0;

                long.TryParse(productNumTemp, out productNum);
                productNum++;

                this.UpdateProductInfo(productNum, ConfigInfo.LineCode2);

                if (ConfigInfo.DtProductInfo2.Rows.Count >= 1)
                {
                    foreach (DataRow row in ConfigInfo.DtProductInfo2.Rows)
                    {
                        row["LineCode"] = ConfigInfo.LineCode2;
                        row["Shift"] = ConfigInfo.GetShift(ConfigInfo.DtLineShift2);
                        row["ProductNum"] = productNum;
                        row["NumTime"] = NumTime2;
                    }
                }
                else
                {
                    DataRow row = ConfigInfo.DtProductInfo2.NewRow();
                    row["LineCode"] = ConfigInfo.LineCode2;
                    row["Shift"] = ConfigInfo.GetShift(ConfigInfo.DtLineShift2);
                    row["ProductNum"] = productNum;
                    row["NumTime"] = NumTime2;

                    ConfigInfo.DtProductInfo2.Rows.Add(row);
                }

                ProductInfo.UpdateProductInfo(ConfigInfo.DtProductInfo2);
            }

            if (ConfigInfo.WatchLines.ToUpper().Contains(ConfigInfo.LineCode3))
            {
                string NumTime3 = LineShift.GetLineNumTime(ConfigInfo.LineCode3);
                string productNumTemp = this.lblActualCompletedLine3.Text.Trim();
                long productNum = 0;

                long.TryParse(productNumTemp, out productNum);
                productNum++;

                this.UpdateProductInfo(productNum, ConfigInfo.LineCode3);

                if (ConfigInfo.DtProductInfo3.Rows.Count >= 1)
                {
                    foreach (DataRow row in ConfigInfo.DtProductInfo3.Rows)
                    {
                        row["LineCode"] = ConfigInfo.LineCode3;
                        row["Shift"] = ConfigInfo.GetShift(ConfigInfo.DtLineShift3);
                        row["ProductNum"] = productNum;
                        row["NumTime"] = NumTime3;
                    }
                }
                else
                {
                    DataRow row = ConfigInfo.DtProductInfo3.NewRow();
                    row["LineCode"] = ConfigInfo.LineCode3;
                    row["Shift"] = ConfigInfo.GetShift(ConfigInfo.DtLineShift3);
                    row["ProductNum"] = productNum;
                    row["NumTime"] = NumTime3;

                    ConfigInfo.DtProductInfo3.Rows.Add(row);
                }

                ProductInfo.UpdateProductInfo(ConfigInfo.DtProductInfo3);
            }
        }

        /// <summary>
        /// Refresh Product Info And Update UI
        /// </summary>
        private void RefreshProductInfoAndUpdateUI()
        {
            this.GetProductInfoAndUpdateUI();
            this.RefreshProductInfoAndSave();
        }

        /// <summary>
        /// Set Screen Saver Timer Enable
        /// </summary>
        /// <param name="enabled"></param>
        public void SetScreenSaverTimerEnable(bool enabled)
        {
            this.timerScreenSaver.Enabled = enabled;
        }

        /// <summary>
        /// Show Screen Saver
        /// </summary>
        private void ShowScreenSaver()
        {
            if (this.screenSaver == null)
            {
                this.screenSaver = new ScreenSaver();
            }

            if (this.screenSaver.IsDisposed)
            {
                this.screenSaver = new ScreenSaver();
            }

            if (!this.screenSaver.Visible)
            {
                this.screenSaver.ProductManagementForm = this;
                this.SetScreenSaverTimerEnable(false);
                this.screenSaver.ShowDialog();
            }
        }

        private void timerReadLineInfo_Tick(object sender, EventArgs e)
        {
            this.GetLineInfoAndUpdateUI();
        }

        private void timerReadProductInfo_Tick(object sender, EventArgs e)
        {
            this.GetProductInfoAndUpdateUI();
        }

        private void timerUpdateProductInfo_Tick(object sender, EventArgs e)
        {
            //this.RefreshProductInfoAndUpdateUI();
        }

        private void timerScreenSaver_Tick(object sender, EventArgs e)
        {
            this.ShowScreenSaver();
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            if ((this.lineInfoSetView == null)
                || this.lineInfoSetView.IsDisposed)
            {
                this.lineInfoSetView = new LineInfoSetView();
            }

            this.lineInfoSetView.ProductManagementForm = this;
            this.lineInfoSetView.Show();
            this.lineInfoSetView.BringToFront();
        }

        private void btnProductSetting_Click(object sender, EventArgs e)
        {
            if ((this.productNumSetView == null)
                || this.productNumSetView.IsDisposed)
            {
                this.productNumSetView = new ProductNumSetView();
            }

            this.productNumSetView.ProductManagementForm = this;
            this.productNumSetView.Show();
            this.productNumSetView.BringToFront();
        }

        private void tlpOperationArea1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}